﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class AddSecurity : Form
    {
        public AddSecurity()
        {
            InitializeComponent();
        }

        private void AddSecurity_Load(object sender, EventArgs e)
        {

        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            Security security = new Security();
            security.Symbol = textBoxSymbol.Text;
            security.Name = textBoxName.Text;
            security.MarginPercentage = double.Parse(textBoxMarginPercentage.Text);
            security.MarketValue = double.Parse(textBoxMarketValue.Text);

            try
            {
                SecurityServices.Add(security);
            }
            catch (System.Exception ex)
            {
                if (ex.Message.StartsWith("ORA-00001: unique"))
                {
                    MessageBox.Show("Security " + textBoxSymbol.Text + " already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show("Failed to create new security\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                return;
            }
            MessageBox.Show("security added successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
            if (checkBoxDontClose.Checked == false)
            {
                Close();
            }
            else
            {
                textBoxSymbol.Text = "";
                textBoxName.Text = "";
                textBoxMarginPercentage.Text = "";
                textBoxMarketValue.Text = "";
                textBoxSymbol.Focus();
            }
        }

        private bool validateInputs()
        {
            if (textBoxSymbol.Text == "")
            {
                MessageBox.Show("Enter symbol", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            double fMarginPercentage = 0;
            if (double.TryParse(textBoxMarginPercentage.Text, out fMarginPercentage) == false)
            {
                MessageBox.Show("Invalid margin percentage", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            double fMarketValue = 0;
            if (double.TryParse(textBoxMarketValue.Text, out fMarketValue) == false)
            {
                MessageBox.Show("Invalid market value", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
